package com.group5.onlinegame.handler;

import com.github.pagehelper.PageInfo;
import com.group5.onlinegame.entity.Recharge;
import com.group5.onlinegame.entity.RechargeRecording;
import com.group5.onlinegame.entity.User;
import com.group5.onlinegame.service.RechargeRecordingService;
import com.group5.onlinegame.service.RechargeService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/")
public class RechargeRecordingHandler {

    @Resource
    private RechargeRecordingService rechargeRecordingService;

    @Resource
    private RechargeService rechargeService;

    @RequestMapping("rechargerecording")
    @RequiresRoles("recharge")
    public ModelAndView getRRList(@RequestParam(name = "pageNum", defaultValue = "1", required = false) Integer cpage,
                                   @RequestParam(name = "input", defaultValue = "0", required = false) String input,
                                   @RequestParam(name = "city", defaultValue = "0", required = false) int city,
                                   @RequestParam(name = "cityi", defaultValue = "0", required = false) String cityi) {
        ModelAndView mv = new ModelAndView("rechargerecording");
        RechargeRecording rechargeRecording = new RechargeRecording();
        User user = new User();
        Recharge recharge = new Recharge();

        if (city == 1) {
            user.setUsername(input);
        } else if (city == 2) {
            recharge.setId(Integer.parseInt(cityi));
        } else if (city == 3) {
            rechargeRecording.setOrderid(input);
        }
        rechargeRecording.setRecharge(recharge);
        rechargeRecording.setUsers(user);
        PageInfo<RechargeRecording> pageInfo = rechargeRecordingService.getRRList(cpage, rechargeRecording);
        mv.addObject("input", input);
        mv.addObject("city", city);
        mv.addObject("cityi", cityi);
        mv.addObject("pageInfo", pageInfo);
        return mv;
    }


    @RequestMapping("delrechargerecording")
    @ResponseBody
    @RequiresRoles("recharge")
    public String delRecharge(@RequestParam(value = "listid[]") int[] listid) {
        try {
            rechargeRecordingService.delRR(listid);
        } catch (Exception e) {
            e.printStackTrace();
            return "1";
        }
        return "0";
    }


    /**/
    @RequestMapping("rechargerecordingid")
    @ResponseBody
    @RequiresRoles("recharge")
    public List<Recharge> rrid() {

        List<Recharge> list = rechargeService.getRechargeList();
        return list;

    }

}
